package com.bankcomm.ui.specialwidget;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.bankcomm.R;
import com.rytong.tools.utils.AsyncImageLoader;
import com.rytong.tools.utils.Utils;
import com.rytong.tools.utils.AsyncImageLoader.ImageCallBack;

public class MostPopShopGallery extends FrameLayout {

	/** 名称 */
	public String attrName;

	/** 所有的图片地址 */
	private ArrayList<String> imageAddres;

	/** 顶部的Gallery */
	private Gallery innerGallery;

	/** 底部的圆圈 */
	private RadioGroup arcs;

	/** 当前的活动 */

	/** 适配器 */
	private GalleryAdapter galleryAdapter = null;

	private LinearLayout bottomLayout;

	private String needRound;


	public MostPopShopGallery(final Context context) {
		super(context);
		
	}
	
	public MostPopShopGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	
	
	public void initLayout(Context context,ArrayList list){
		imageAddres = list;
		innerGallery = new Gallery(context);
		arcs = new RadioGroup(context);
		arcs.setOrientation(RadioGroup.HORIZONTAL);
		reDrawArcs(imageAddres);
		this.setBackgroundColor(Color.TRANSPARENT);
		galleryAdapter = new GalleryAdapter(context, imageAddres, innerGallery,
				needRound);

		innerGallery.setAdapter(galleryAdapter);
		innerGallery.setHorizontalFadingEdgeEnabled(false);
		innerGallery.setSelection(0);
		innerGallery.setSpacing(0);
		innerGallery.setFadingEdgeLength(0);
		innerGallery.setUnselectedAlpha(1.0f);
		innerGallery
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						arcs.check(arg2 % imageAddres.size());
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {

					}

				});

		// 添加到容器
		addView(innerGallery, new LayoutParams(-1, -1));

		// 添加到容器
		LayoutParams arcLayout = new LayoutParams(-2,
				-2);
		arcLayout.gravity = Gravity.CENTER;
		arcLayout.rightMargin = 50;

		FrameLayout.LayoutParams btmLayout = new LayoutParams(-1,
				25);
		btmLayout.gravity = Gravity.BOTTOM;

		bottomLayout = new LinearLayout(context);
		bottomLayout.setBackgroundColor(android.graphics.Color.argb(150, 0, 0,
				0));
		bottomLayout.addView(arcs, arcLayout);
		bottomLayout.setGravity(Gravity.CENTER );
		addView(bottomLayout, btmLayout);
	}

	/**
	 * 构造所有的底部小圆
	 * 
	 * @param imgUrls
	 *            图片地址列表, 以分号分割.
	 */
	public void reDrawArcs(ArrayList<String> list) {
		if (list == null) {
			return;
		}
		if (arcs == null) {
			arcs = new RadioGroup(getContext());
		}
		arcs.removeAllViews();

//		for (String s : list) {
//			if (!"".equals(s)) {
//				this.imageAddres.add(s);
//			}
//		}

		RadioGroup.LayoutParams radioLayout = new RadioGroup.LayoutParams(20,
				20);
		for (int i = 0; i < list.toArray().length; i++) {
			RadioButton rb = new RadioButton(getContext());
			rb.setId(i);
			rb.setButtonDrawable(android.R.color.transparent);
			rb.setClickable(false);
			rb.setBackgroundResource(R.drawable.adv_gallery_mark_selector);
			this.arcs.addView(rb, radioLayout);
		}
	}

	class GalleryAdapter extends BaseAdapter {

		private Context context = null;
		private Gallery gallery = null;
		private AsyncImageLoader asyncImageLoader;
		private String needRound;

		public GalleryAdapter(Context context, List<String> imgURL,
				Gallery currGallery, String needRoundStr) {
			this.context = context;
			this.gallery = currGallery;
			asyncImageLoader = Utils.getAsyncImageLoader();
			needRound = needRoundStr;
		}

		@Override
		public int getCount() {
			 return imageAddres.size();
//			return Integer.MAX_VALUE;
		}

		@Override
		public Object getItem(int position) {
			return imageAddres.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = null;
			if (imageAddres.size() > 0) {
				imageView = new ImageView(context);

				String curr_URL = imageAddres.get(position % imageAddres.size());
				imageView.setTag(curr_URL);
				
				//绑定图片，下载
				asyncImageLoader.downloadBitmap(context,curr_URL,callBack);
				Bitmap bitmap = null;	
				bitmap = asyncImageLoader.getBitmapFromCache(curr_URL);
				if(null != bitmap){
					imageView.setImageBitmap(bitmap);
				}else{
					imageView.setImageResource(R.drawable.img_waiting);
				}
				// 设置边界对齐
				// imageView.setAdjustViewBounds(true);
				// 设置比例类型
				imageView.setScaleType(ImageView.ScaleType.FIT_XY);
				imageView.setLayoutParams(new Gallery.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, -1));
			}
			return imageView;
		}
		

	}
	/**
	 * 图片下载的回调接口
	 */
	private ImageCallBack callBack = new ImageCallBack() {
		
		@Override
		public void loadImageSuccess(Bitmap bitmap, String url) {
			// TODO Auto-generated method stub
			galleryAdapter.notifyDataSetChanged();
		}
	};

}
